package com.hy.lightning.boot.web.servlet.impl;

import com.hy.lightning.boot.core.RequestException;
import com.hy.lightning.commons.utils.ObjectUtil;

/**
 * 格式化servletPath
 * @author yello
 *
 */
public class URLPathHandler {
	private String serviceName;//请求的服务名称

	private String method;//请求服务的方法名称

	private void formaitPath(String servletPath) throws RequestException {
		if(ObjectUtil.isNullOrEmpty(servletPath)) throw new RequestException(0, "", "请求路径不合法 ,标准格式http://ip:port/service/method");
		if (servletPath.startsWith("/"))
			servletPath = servletPath.substring(1);
		String[] paths = servletPath.split("/");
		if (paths == null || paths.length != 2)
			throw new RequestException(0, "", "请求路径不合法 ,标准格式http://ip:port/service/method");
		serviceName = paths[0];
		method = paths[1];
	}
	
	public URLPathHandler(String servletPath) throws RequestException {
		formaitPath(servletPath);
	}

	public String getServiceName() {
		return serviceName;
	}

	public String getMethod() {
		return method;
	}
	
	public String getRequestService() {
		return serviceName + "/" + method;
	}
	
}
